\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f2\b0\i0\ul0\fs28\fc0 C++ integration\
\
Q: Can I integrate C++ code into my Interface Builder/Objective-C application? How?\
\
A: Yes, in release 2.0 you can, and it's pretty easy (once you know how)! The procedure breaks down into three categories of things that you must do: compiling, Interface Builder and getting the two languages to talk to each other.\
\
\b Compiling
\b0 \
First, you must use the C++ compiler for
\i all
\i0 of your source files--including the Objective-C sources. To do this, add the following line to your Makefile.preamble:\
\f0 \
CC=cc++\
\f2 \
Now that you are using the C++ compiler, you have to notify the compiler when/if your header files contain non-C++ code. For Objective-C header files, encapsulate your
\f0 #import
\f2 directives like this:\
\
\f0 extern "Objective-C" \
\{ \
#import <appkit/Application.h>\
#import <appkit/Panel.h>\
#import <appkit/TextField.h>\
#import <appkit/Button.h>\
\}\
\f2 \
For regular C header files, encapsulate your
\f0 #import
\f2 directives like this: \
\
\f0 extern "C"\
\{\
#import <appkit/publicWraps.h>\
#import <objc/error.h>\
#import <objc/NXStringTable.h>\
#import <strings.h>\
\}\
\
\pard\tx620\tx1240\tx1860\tx2480\tx3100\tx3720\tx4340\tx4980\tx5600\tx6220\f2\fc0 The C++ "linkage" directive serves two purposes (when importing interface files that contain straight ANSI-C/Objective-C code). It:\
\pard\tx620\tx1240\tx1860\tx2480\tx3100\tx3720\tx4340\tx4980\tx5600\tx6220\fc0 allows you to link with libraries that have not been compiled with the C++ compiler. Since libraries on the NeXT computer are compiled with the Objective-C compiler (cc, not cc++), you must use the C++ linkage directive when importing interface files that represent NeXT libraries (or any library that is not compiled with cc++). \
\pard\tx620\tx1240\tx1860\tx2480\tx3100\tx3720\tx4340\tx4980\tx5600\tx6220\fc0 tells the compiler to ignore C++ keywords that will result in syntax errors when importing ANSI-C
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 or
\pard\tx620\tx1240\tx1860\tx2480\tx3100\tx3720\tx4340\tx4980\tx5600\tx6220\fc0 Objective-C interface files. The linkage directive essentially tells the C++ compiler to treat keywords (such as
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 the method names
\pard\tx620\tx1240\tx1860\tx2480\tx3100\tx3720\tx4340\tx4980\tx5600\tx6220\fc0 "new", "delete", etc.) as normal identifiers.\
Since the nib files generated by Interface Builder are based the appkit, and it generates source templates in Objective-C, we must envision our program such that Objective-C and nib files are the foundation of our program, and the C++ code is a supporting library.\
\
Now that we can compile, we need to get an Objective-C object and a C++ object to pass messages to one another. Suppose that we have two created objects--a C++ object and an Objective-C object. This is how you
\b
\b0 would refer to the C++ object and tell it to "do something":\
\pard\tx620\tx1240\tx1860\tx2480\tx3100\tx3720\tx4340\tx4980\tx5600\tx6220\f0\fc0 class CalcEngine *cplus_object; \
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\fc0 cplus_object = new CalcEngine;\
\f2
\f0 cplus_object->doSomething();
\f2 \
\
C++ objects are implemented as regular C structures, so to access public instance variables, or public methods of a C++ object, you dereference the object with the -> syntax as you would a structure member. And this is how you would refer to an Objective-C object from C++:\
\
\f0 id objectiveObj;\
\
objectiveObj = [ObjectiveObjCls new];\
[objectiveObj doSomethingElse:what];\
\f2 \
Basically, in either case you use the language constructs of the object to which you are referring, and embed them in the source file of the other language.\
\
\b Example
\b0 \
There is an example located in /NextDeveloper/Examples/Calculator++ which illustrates the integration of Interface Builder nib files, Objective-C source code, and C++ source code into one program.\
\
QA584\
\
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 Valid for 2.0\